<template>
  <div class="app-container">
  <div class="filter-container">
        <el-button type="primary" @click="addClick()" style="margin-left:0px;">新增</el-button>
        <el-input clearable v-model="search.number" placeholder="维修类别名称" style="width: 170px;margin-left:10px;"></el-input>

        <el-button type="primary" icon="el-icon-search" @click="searchClick()" style="margin-left:10px;">查询</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" 
      border fit highlight-current-row stripe 
      style="width: 100%">
      <el-table-column align="center" label="序" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row._id }}</span>
        </template>
      </el-table-column>
     <el-table-column  min-width="100" align="center" label="维修类别名称">
        <template slot-scope="scope">
          <span>{{ scope.row.itemname }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="维修类型">
        <template slot-scope="scope">
          <span>{{ scope.row.fittype | parseFixType }}</span>
        </template>
      </el-table-column>
      
      
      <el-table-column  min-width="100" align="center" label="状态">
        <template slot-scope="scope">
          
          <el-switch v-model="scope.row.state" active-color="#13ce66" inactive-color="#aaaaaa"></el-switch>
          <span>{{ scope.row.state==1 ?'启用':'禁用' }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作"  width="120" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="rowEditClick(scope.row.id)">修改</el-button>
          
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.rows" @pagination="fetchData" />

<!-- 新增编辑 -->
<el-dialog :title="dialogEdit.title" :visible.sync="dialogEdit.visible"  width="900px">

    <div class="ci">
        <div class="item">
            <span class="name required">类别名称：</span>
            <span class="value">
                <el-input v-model="dialogEdit.data.itemname" placeholder="" style="width:220px;"></el-input>
            </span>
        </div>
        <div class="item">
            <span class="name required">维修类型</span>
            <span class="value">
                <el-select v-model="dialogEdit.data.fixtype" clearable placeholder="--" style="width: 130px;">
                    <el-option 
                    v-for="(item,index) in selectOptions.fixtype"
                    :key="index"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
            </span>
        </div>
        
        
    </div>
    <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEdit.visible = false">取 消</el-button>
        <el-button type="primary"  @click="dgAddSaveClick()" :loading="dialogEdit.saveloading">保 存</el-button>
    </div>
</el-dialog>
<!-- end 新增编辑 -->


  </div>
</template>

<script>
import * as settingApi from '@/api/aftersale/setting'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import * as enumHelper from '@/utils/enum'
import {objectMerge} from '@/utils/index'

export default {
    components: { Pagination },
    data(){
        return{
          search:{ 
              name:'',
          },
          listQuery: {
                page: 1,
                rows: 20,
                name:'',
          },
          list:null,
          listLoading:false,
          total:0,
          selectOptions:{
            fixtype:[]
          },

          dialogEdit:{
              visible:false,
              saveloading:false,
              title:'新增/编辑',
              data:{
                  id:'',
                  itemname:'',
                  worktype:'',
              },
          },
          
        }
    },
    created(){
        var that=this;
        //初始化下拉菜单
        enumHelper.initOptions(that.selectOptions.fixtype,enumHelper.aftersale.fixType);
        that.fetchData();
    },
    filters:{
      parseFixType(val){
        return enumHelper.aftersale.fixType.getName(val);
      },
      parseFixCategory(val){
        return enumHelper.aftersale.fixCategory.getName(val);
      },
      parseCommissionState(val){
        return enumHelper.aftersale.commissionState.getName(val);
      }

    },
    methods:{
        fetchData(){
          var that=this;
            settingApi.getFixcategoryList({}).then(res=>{
              if(res.data){
                that.list=[...res.data.items.map((item,index)=>{
                  item._id=index+1;
                  return item;
                })];
              }
            })
        },
        refreshClick(){
          this.fetchData();
        },
        
        addClick(){
          var that=this;
          that.dialogEdit.visible=true;
          //objectMerge(that.dialogEdit.data,{});
          that.dialogEdit.data.itemname='';
        },
        
        
        dgAddSaveClick(){
          this.dialogEdit.saveloading=true;
          setTimeout(()=>{
              this.dialogEdit.visible=false;
          },1000)
        },
        
        rowEditClick(id){
           var that=this;
           var selectItem=that.list.filter(o=>id==id)[0];
           objectMerge(that.dialogEdit.data,selectItem);
           that.dialogEdit.visible=true;
        },
        
    }
}
</script>

<style>

</style>